package com.ts.controller.achievement;

import com.ts.constant.AchievementConstant;
import com.ts.dto.SoftwareCopyrightDTO;
import com.ts.commom.Result;
import com.ts.service.achievement.SoftwareCopyrightService;
import com.ts.service.achievement.TsAchievementService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 软件著作权相关接口 - 软件著作权相关成果的提交，查询
 */
@RestController
@RequestMapping("/achievement/softwareCopyright")
@Tag(name = "软件著作权相关接口", description = "软件著作权的提交和查询功能")
@Slf4j
public class SoftwareCopyrightController {

    @Autowired
    private final SoftwareCopyrightService softwareCopyrightService;

    @Autowired
    private  TsAchievementService tsAchievementService;
    /**
     * 构造函数注入（推荐方式）
     */
    @Autowired
    public SoftwareCopyrightController(SoftwareCopyrightService softwareCopyrightService) {
        this.softwareCopyrightService = softwareCopyrightService;
    }

    /**
     * 提交软件著作权
     *
     * @param softwareCopyrightDTO 软件著作权数据传输对象
     * @return 操作结果
     */
    @Operation(summary = "提交", description = "提交软件著作权")
    @PostMapping()
    public Result<String> saveSoftwareCopyright(@RequestBody SoftwareCopyrightDTO softwareCopyrightDTO) {
        log.info("softwareCopyrightDTO:{}", softwareCopyrightDTO);
        try {
            // 参数验证
            if (softwareCopyrightDTO.getDevelopmentCompletionTime() == null) {
                return Result.error("开发完成时间不能为空");
            }
            if (softwareCopyrightDTO.getSoftwareName() == null || softwareCopyrightDTO.getSoftwareName().trim().isEmpty()) {
                return Result.error("软件名称不能为空");
            }
            if (softwareCopyrightDTO.getCopyrightRegistrationNumber() == null || softwareCopyrightDTO.getCopyrightRegistrationNumber().trim().isEmpty()) {
                return Result.error("软件著作权登记号不能为空");
            }

            // 保存软著并获取数据库生成的ID
            Long generatedId = softwareCopyrightService.save(softwareCopyrightDTO);

            String type = AchievementConstant.SOFTWARE_COPYRIGHT;
            String achievename = softwareCopyrightDTO.getSoftwareName();
            String descriptipn = "9635";
            Integer level = 2;
            Integer  createdBy = 1;
            Long softlId = generatedId;
            tsAchievementService.addAchievement(achievename
            ,descriptipn
            ,type
            ,level
            ,createdBy
            ,softlId);


            return Result.success("提交成功", "操作成功"); // 确保返回有数据的Result
        } catch (Exception e) {
            log.error("提交软件著作权失败", e);
            return Result.error("提交失败: " + e.getMessage());
        }
    }

    /**
     * 根据ID查询软件著作权详情
     *
     * @param id 软件著作权ID
     * @return 软件著作权详情
     */
    @Operation(summary = "根据ID查询", description = "根据ID查询软件著作权详情")
    @GetMapping("/{id}")
    public Result<SoftwareCopyrightDTO> getById(@PathVariable Long id) {
        log.info("查询软件著作权ID: {}", id);
        try {
            SoftwareCopyrightDTO softwareCopyrightDTO = softwareCopyrightService.getById(id);
            if (softwareCopyrightDTO == null) {
                return Result.error( "软件著作权不存在");
            }
            return Result.success(softwareCopyrightDTO);
        } catch (Exception e) {
            log.error("查询软件著作权失败", e);
            return Result.error("查询失败");
        }
    }

    /**
     * 更新软件著作权
     *
     * @param softwareCopyrightDTO 软件著作权数据传输对象
     * @return 操作结果
     */
    @Operation(summary = "更新", description = "更新软件著作权")
    @PutMapping()
    public Result<String> updateSoftwareCopyright(@RequestBody SoftwareCopyrightDTO softwareCopyrightDTO) {
        log.info("更新软件著作权: {}", softwareCopyrightDTO);
        try {
            softwareCopyrightService.update(softwareCopyrightDTO);
            return Result.success("更新成功");
        } catch (Exception e) {
            log.error("更新软件著作权失败", e);
            return Result.error("更新失败");
        }
    }

    /**
     * 根据ID删除软件著作权
     *
     * @param id 软件著作权ID
     * @return 操作结果
     */
    @Operation(summary = "删除", description = "根据ID删除软件著作权")
    @DeleteMapping("/{id}")
    public Result<String> deleteSoftwareCopyright(@PathVariable Long id) {
        log.info("删除软件著作权ID: {}", id);
        try {
            softwareCopyrightService.delete(id);
            return Result.success("删除成功");
        } catch (Exception e) {
            log.error("删除软件著作权失败", e);
            return Result.error("删除失败");
        }
    }

    /**
     * 查询所有软件著作权
     *
     * @return 软件著作权列表
     */
    @Operation(summary = "查询所有", description = "查询所有软件著作权")
    @GetMapping("/all")
    public Result<List<SoftwareCopyrightDTO>> getAll() {
        try {
            List<SoftwareCopyrightDTO> list = softwareCopyrightService.getAll();
            return Result.success(list);
        } catch (Exception e) {
            log.error("查询所有软件著作权失败", e);
            return Result.error("查询失败");
        }
    }

    /**
     * 根据软件名称查询
     *
     * @param softwareName 软件名称
     * @return 软件著作权列表
     */
    @Operation(summary = "根据软件名称查询", description = "根据软件名称模糊查询")
    @GetMapping("/search")
    public Result<List<SoftwareCopyrightDTO>> getBySoftwareName(@RequestParam String softwareName) {
        try {
            List<SoftwareCopyrightDTO> list = softwareCopyrightService.getBySoftwareName(softwareName);
            return Result.success(list);
        } catch (Exception e) {
            log.error("根据软件名称查询失败", e);
            return Result.error("查询失败");
        }
    }
}

